package org.jeecg.extra.aspect;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * (概要说明).
 *
 * @author chao.xie
 * @version 1.0.0
 * @创建日期: 2024.01.14
 */
@Aspect
@Component
public class ExecutionTimeAspect {
    @Pointcut("@annotation(org.jeecg.extra.aspect.annotation.LogExecutionTime)")
    public void annotationPointCutDefinition() {
    }

    @Around("annotationPointCutDefinition()")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        if(!StrUtil.equals(SpringUtil.getActiveProfile(), "dev")){
            return joinPoint.proceed();
        }

        long startTime = System.currentTimeMillis();

        // 执行目标方法
        Object proceed = joinPoint.proceed();

        long endTime = System.currentTimeMillis();
        Console.log("\n方法: {} 执行时间: {}ms", joinPoint.getSignature(), (endTime - startTime));
        return proceed;
    }
}
